<html xmlns:th="http://www.thymeleaf.org">
<head th:replace="admin/public/form::head(_)"/>
<body th:replace="admin/public/form::body(~{::div})">
<div class="layui-card">
    <div class="layui-card-header ">
        <span class="layui-breadcrumb">
          <a href="/admin" target="_blank">首页</a>
          <a href="javascript:;">用户角色权限管理</a>
          <a><cite>角色管理</cite></a>
        </span>
        <a class="layui-btn layui-btn-sm" style="line-height:1.6em;margin-top:3px;float:right"
           href="javascript:location.replace(location.href);" title="刷新">
            <i class="layui-icon layui-icon-refresh-3" style="line-height:30px"></i></a>
    </div>
    <div class="layui-card-body ">
        <button class="layui-btn layui-btn-danger layui-btn-sm delAll"><i class="layui-icon"></i>批量删除</button>
        <a class="layui-btn layui-btn-sm" onclick="parent.xadmin.add_tab('添加角色','/admin/role/add',true)"><i class="layui-icon"></i>添加</a>
    </div>
    <div class="layui-card-body ">
        <table class="layui-hide" id="test" lay-filter="demo"></table>
        <script type="text/html" id="barDemo">
            <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i
                    class="layui-icon layui-icon-edit"></i>编辑</a>
            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i
                    class="layui-icon layui-icon-delete"></i>删除</a>
        </script>
    </div>
</div>
</body>
<script>
    layui.use(['table', 'jquery', 'form', 'element'], function () {
        var table = layui.table
        $ = layui.jquery,
            form = layui.form;
        layui.element.render();
        table.render({
            elem: '#test'
            , url: '/admin/role/tree'
            , cellMinWidth: 70
            , cols: [
                [
                    {type: 'checkbox'}
                    , {field: 'id', title: 'ID', width: 100, unresize: true, sort: true}
                    , {field: 'title', title: '用户名', templet: '#usernameTpl'}
                    , {field: 'notation', title: '描述'}
                    , {
                    title: '状态', align: 'center'
                    , templet: function (d) {
                        let sel = d.status == 1 ? 'checked' : '';
                        return '<input type="checkbox" name="status" lay-filter="status" value="' + d.id + '" lay-skin="switch" lay-text="启用|禁用"' + sel + '>';
                    }
                }
                    , {title: '操作', align: 'center', toolbar: '#barDemo'}
                ]
            ]
            , page: true
        });
        //监听工具条
        table.on('tool(demo)', function (obj) {
            var data = obj.data;
            switch (obj.event) {
                case 'detail':
                    layer.msg('ID：' + data.id + ' 的查看操作');
                    break;
                case 'del':
                    layer.confirm('确定要删除该角色吗？', function (index) {
                        $.ajax({
                            url: '/admin/role/delete',
                            method: 'post',
                            data: {id: data.id},
                            dataType: 'JSON',
                            success: function (res) {
                                if (res.code == 0) {
                                    obj.del();
                                    layer.msg('删除成功', {icon: 1});
                                } else {
                                    layer.msg(res.msg, {icon: 5});
                                }
                            },
                            error: function (data) {
                                layer.msg('服务器繁忙', {icon: 5});
                            }
                        });
                        layer.close(index);
                    });
                    break;
                case 'edit':
                    xadmin.open('编辑', '/admin/role/edit?id=' + data.id);
                    // layer.alert('编辑行：<br>'+ JSON.stringify(data))
                    break;
                default:
                    break;
            }
        });
        //ajax禁用开启
        form.on('switch(status)', function (obj) {
            // layer.tips(this.value + ' ' + this.name + '：' + obj.elem.checked, obj.othis);
            let data = {};
            data[this.name] = obj.elem.checked ? 1 : 0;
            let id = this.value;
            let __this = this;
            $.ajax({
                url: '/admin/role/ajax?id=' + id,
                method: 'post',
                data: data,
                dataType: 'JSON',
                success: function (res) {
                    if (res.code == 0) {
                        layer.msg('修改成功', {icon: 1});
                    } else {
                        __this.checked = true;
                        form.render();
                        layer.msg(res.msg, {icon: 5});
                    }
                },
                error: function (data) {
                    layer.msg('服务器繁忙', {icon: 5});
                }
            });
        });

        $('.delAll').on('click', function () {
            var checkStatus = table.checkStatus('test')
                , data = checkStatus.data
                , ids = '';
            if (data.length > 0) {
                for (var i = 0; i < data.length; i++) {
                    ids = ids + data[i].id + ',';
                }
                var ids = (ids.substring(ids.length - 1) == ',') ? ids.substring(0, ids.length - 1) : ids;
                layer.confirm('确定要删除' + data.length + '个角色吗？', function (index) {
                    $.ajax({
                        url: '/admin/role/delete',
                        method: 'post',
                        data: {id: ids},
                        dataType: 'JSON',
                        success: function (res) {
                            if (res.code == 0) {
                                table.reload('test');
                                layer.msg('删除成功', {icon: 1});
                            } else {
                                layer.msg(res.msg, {icon: 5});
                            }
                        },
                        error: function (data) {
                            layer.msg('服务器繁忙', {icon: 5});
                        }
                    });
                    layer.close(index);
                });
            }
        });

    });
</script>
</html>